﻿using System;
using System.Collections.Generic;

namespace Bee.Infrastructure
{
    public class MQueue<T> : Queue<T>
    {
        /// <summary>
        /// 入队事件
        /// </summary>
        public event EventHandler<MQueueEventArgs<T>> OnEnqueue = null;

        /// <summary>
        /// 出队事件
        /// </summary>
        public event EventHandler<MQueueEventArgs<T>> OnDequeue = null;

        /// <summary>
        /// 入队列
        /// </summary>
        /// <param name="item"></param>
        public new void Enqueue(T item)
        {
            base.Enqueue(item);
            if (OnEnqueue != null)
            {
                OnEnqueue.Invoke(this, new MQueueEventArgs<T>(item, this.Count));
            }
        }

        /// <summary>
        /// 出队列
        /// </summary>
        /// <returns></returns>
        public new T Dequeue()
        {
            T item = base.Dequeue();
            if (OnDequeue != null)
            {
                OnDequeue.Invoke(this, new MQueueEventArgs<T>(item, this.Count));
            }
            return item;
        }
    }

    public class MQueueEventArgs<T> : EventArgs
    {
        public MQueueEventArgs(T item, int index)
        {
            this.Item = item;
            this.Index = index;
        }
        public T Item { get; set; }
        public int Index { get; set; }
    }
}
